//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.config.service.impl;

import com.ht.utils.oConvertUtils;
import com.ht.modules.system.online.config.service.DbTableHandleI;
import com.ht.modules.system.online.config.util.ColumnMeta;

public class DbTableSQLServerHandleImpl implements DbTableHandleI {
    public DbTableSQLServerHandleImpl() {
    }

    public String getAddColumnSql(ColumnMeta columnMeta) {
        return " ADD  " + this.getAddFieldDesc(columnMeta) + ";";
    }

    public String getReNameFieldName(ColumnMeta columnMeta) {
        return "  sp_rename '" + columnMeta.getTableName() + "." + columnMeta.getOldColumnName() + "', '" + columnMeta.getColumnName() + "', 'COLUMN';";
    }

    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return " ALTER COLUMN  " + this.getUpdateFieldDesc(cgformcolumnMeta, datacolumnMeta) + ";";
    }

    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String result = "";
        if (dataType.equalsIgnoreCase("varchar")) {
            result = "string";
        } else if (dataType.equalsIgnoreCase("float")) {
            result = "double";
        } else if (dataType.equalsIgnoreCase("int")) {
            result = "int";
        } else if (dataType.equalsIgnoreCase("Date")) {
            result = "date";
        } else if (dataType.equalsIgnoreCase("Datetime")) {
            result = "date";
        } else if (dataType.equalsIgnoreCase("numeric")) {
            result = "bigdecimal";
        } else if (dataType.equalsIgnoreCase("varbinary")) {
            result = "blob";
        }

        return result;
    }

    public String dropTableSQL(String tableName) {
        return " DROP TABLE " + tableName + " ;";
    }

    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName + ";";
    }

    private String getUpdateFieldDesc(ColumnMeta cgfromcolumnMeta, ColumnMeta datacolumnMeta) {
        String result = "";
        if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("string")) {
            result = cgfromcolumnMeta.getColumnName() + " varchar(" + cgfromcolumnMeta.getColumnSize() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("date")) {
            result = cgfromcolumnMeta.getColumnName() + " datetime " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("int")) {
            result = cgfromcolumnMeta.getColumnName() + " int " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("double")) {
            result = cgfromcolumnMeta.getColumnName() + " float " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("bigdecimal")) {
            result = cgfromcolumnMeta.getColumnName() + " numeric(" + cgfromcolumnMeta.getColumnSize() + "," + cgfromcolumnMeta.getDecimalDigits() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("text")) {
            result = cgfromcolumnMeta.getColumnName() + " text " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("blob")) {
            result = cgfromcolumnMeta.getColumnName() + " varbinary(" + cgfromcolumnMeta.getColumnSize() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        }

        return result;
    }

    private String getAddFieldDesc(ColumnMeta cgfromcolumnMeta) {
        String result = "";
        if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("string")) {
            result = cgfromcolumnMeta.getColumnName() + " varchar(" + cgfromcolumnMeta.getColumnSize() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("date")) {
            result = cgfromcolumnMeta.getColumnName() + " datetime " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("int")) {
            result = cgfromcolumnMeta.getColumnName() + " int " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("double")) {
            result = cgfromcolumnMeta.getColumnName() + " float " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("bigdecimal")) {
            result = cgfromcolumnMeta.getColumnName() + " numeric(" + cgfromcolumnMeta.getColumnSize() + "," + cgfromcolumnMeta.getDecimalDigits() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("text")) {
            result = cgfromcolumnMeta.getColumnName() + " text " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("blob")) {
            result = cgfromcolumnMeta.getColumnName() + " varbinary(" + cgfromcolumnMeta.getColumnSize() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        }

        return result;
    }

    private String getRenameFieldDesc(ColumnMeta cgfromcolumnMeta) {
        String result = "";
        if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("string")) {
            result = cgfromcolumnMeta.getColumnName() + " varchar(" + cgfromcolumnMeta.getColumnSize() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("date")) {
            result = cgfromcolumnMeta.getColumnName() + " datetime " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("int")) {
            result = cgfromcolumnMeta.getColumnName() + " int " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("double")) {
            result = cgfromcolumnMeta.getColumnName() + " float " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        }

        return result;
    }

    public String getCommentSql(ColumnMeta columnMeta) {
        StringBuffer commentSql = new StringBuffer("EXECUTE ");
        if (oConvertUtils.isEmpty(columnMeta.getOldColumnName())) {
            commentSql.append("sp_addextendedproperty");
        } else {
            commentSql.append("sp_updateextendedproperty");
        }

        commentSql.append(" N'MS_Description', '");
        commentSql.append(columnMeta.getComment());
        commentSql.append("', N'SCHEMA', N'dbo', N'TABLE', N'");
        commentSql.append(columnMeta.getTableName());
        commentSql.append("', N'COLUMN', N'");
        commentSql.append(columnMeta.getColumnName() + "'");
        return commentSql.toString();
    }

    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return null;
    }

    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName + " ON " + tableName;
    }
}
